import 'package:bi_project/http/dao/home_dao.dart';
import 'package:bi_project/model/home_mo.dart';
import 'package:bi_project/navigator/hi_navigator.dart';
import 'package:bi_project/page/home_tab_page.dart';
import 'package:bi_project/utils/color.dart';
import 'package:bi_project/utils/screenAdapter.dart';
import 'package:flutter/material.dart';
import 'package:underline_indicator/underline_indicator.dart';

import '../widget/navigation_bar.dart';

class HomePage extends StatefulWidget {
  final ValueChanged<int>? onJumpTo;
  const HomePage({super.key, this.onJumpTo});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  var listener;
  List<CategoryListMo> categoryList = [];
  List<BannerListMo> bannerList = [];
  late TabController _controller;
  @override
  void initState() {
    super.initState();
    _controller = TabController(length: categoryList.length, vsync: this);
    HiNavigator.getInstance().addListener(listener = (current, pre) {
      print("current:${current.page}");
      print("pre:${pre.page}");
      print("widget:${widget}");
      if (widget == current.page || current.page is HomePage) {
        print("打开的首页：onresume");
      } else if (widget == pre?.page || pre?.page is HomePage) {
        print("关闭的首页：onpause");
      }
    });
    loadData();
  }

  @override
  void dispose() {
    HiNavigator.getInstance().removeListener(listener);
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: SizedBox(
        child: Column(
          children: [
            NavigationBarWidget(
              height: 50,
              color: Colors.white,
              statusStyle: StatusStyle.DARK_CONTENT,
              child: _appBar(),
            ),
            Container(
              color: Colors.white,
              child: _tabBar(),
            ),
            Flexible(
                child: TabBarView(
                    controller: _controller,
                    children: categoryList
                        .map((tab) => HomeTabPage(
                              name: tab.name,
                              bannerList: tab.name == '推荐' ? bannerList : null,
                            ))
                        .toList()))
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  _appBar() {
    return Padding(
      padding: const EdgeInsets.only(left: 15, right: 15),
      child: Row(
        children: [
          InkWell(
            onTap: () {
              if (widget.onJumpTo != null) {
                widget.onJumpTo!(3);
              }
            },
            child: ClipRRect(
              borderRadius: BorderRadius.circular(23),
              child: const Image(
                  width: 46,
                  height: 46,
                  image: AssetImage("images/avatar.jfif")),
            ),
          ),
          Expanded(
              child: Padding(
            padding: const EdgeInsets.only(left: 15, right: 15),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(16),
              child: Container(
                height: 32,
                padding: const EdgeInsets.only(left: 10),
                decoration: BoxDecoration(color: Colors.grey.shade100),
                alignment: Alignment.centerLeft,
                child: const Icon(
                  Icons.search,
                  color: Colors.grey,
                ),
              ),
            ),
          )),
          const Icon(
            Icons.explore_outlined,
            color: Colors.grey,
          ),
          const Padding(
            padding: EdgeInsets.only(left: 10),
            child: Icon(
              Icons.mail_outline,
              color: Colors.grey,
            ),
          )
        ],
      ),
    );
  }

  _tabBar() {
    return TabBar(
        controller: _controller,
        isScrollable: true,
        labelColor: Colors.black,
        indicator: const UnderlineIndicator(
          strokeCap: StrokeCap.round,
          borderSide: BorderSide(color: primary, width: 3),
          insets: EdgeInsets.only(left: 15, right: 15, bottom: 5),
        ),
        tabs: categoryList.map((tab) {
          return Tab(
            child: Padding(
              padding: const EdgeInsets.only(left: 5, right: 5),
              child: Text(
                tab.name,
                style: TextStyle(fontSize: ScreenAdapter.fontSize(16)),
              ),
            ),
          );
        }).toList());
  }

  void loadData() async {
    try {
      HomeMo result = await HomeDao.get("推荐");
      if (result.categoryList != null) {
        _controller =
            TabController(length: result.categoryList.length, vsync: this);
      }
      setState(() {
        categoryList = result.categoryList;
        bannerList = result.bannerList;
      });
    } catch (e) {
      print(e);
    }
  }
}
